package com.ylw;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Properties;

public class GetIp {
    private static InetAddress localNetIp = null;
    public static InetAddress getLocalNetIp() {
        if (localNetIp != null) {
            return localNetIp;
        } else {
            InetAddress localip = null;
            InetAddress netip = null;

            try {
                Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
                InetAddress ip = null;
                boolean finded = false;

                while(netInterfaces.hasMoreElements() && !finded) {
                    NetworkInterface ni = (NetworkInterface)netInterfaces.nextElement();
                    Enumeration address = ni.getInetAddresses();

                    while(address.hasMoreElements()) {
                        ip = (InetAddress)address.nextElement();
                        System.out.println(ni.getName() + ";" + ip.getHostAddress() + ";ip.isSiteLocalAddress()=" + ip.isSiteLocalAddress() + ";ip.isLoopbackAddress()=" + ip.isLoopbackAddress());
                        if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                            netip = ip;
                            finded = true;
                            break;
                        }

                        if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
                            localip = ip;
                        }
                    }
                }
            } catch (SocketException var7) {
            }

            if (netip != null) {
                localNetIp = netip;
                return netip;
            } else {
                localNetIp = localip;
                return localip;
            }
        }
    }

    public static void main(String[] args) {
//        System.out.println(getLocalNetIp());
        Enumeration keys  = System.getProperties().keys();
        while(keys.hasMoreElements()){
            String key  = (String) keys.nextElement();
            System.out.println(key +" "+ System.getProperty(key));
        }
    }
}
